package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.entity.Newscategory;

import com.ysd.impl.NewscategoryDaolmpl;
import com.ysd.utli.BaseServlet;
import com.ysd.utli.Result;
 
public class AdminNewsCategoryServlet extends BaseServlet {
	 
	/** 获取产品分类
	 * localhost:8080/dayixiangmu/adminNewsCategory?reqName=getCategory
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewscategoryDaolmpl nDao=new NewscategoryDaolmpl();//创建实现类对象
		List<Newscategory>nList= nDao.getNewscategory();// 所有产品分类
		
		response.setCharacterEncoding("UTF-8");//响应编码格式
		PrintWriter out = response.getWriter();//out对象
		
		Gson gson = new Gson();//创建Gson对象
		
		out.print(gson.toJson(nList));//将nList转化为json格式字符串并响应数据
		
		out.flush();
		out.close();
		
	}
	/**
	 * 删除产品分类
	 * localhost:8080/dayixiangmu/adminNewsCategory?reqName=delCateoryId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delCateoryId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接收前台传来的参数
		int id=Integer.parseInt(request.getParameter("id"));// 接收前台传来的参数
		NewscategoryDaolmpl nDao=new NewscategoryDaolmpl();// 创建实现类对象
		
		response.setCharacterEncoding("UTF-8");// 设置编码响应格式
		PrintWriter out=response.getWriter();// out对象
		int n = nDao.delCateoryId(id);// 调用实现类的删除方法,返回受影响行数
		
		if (n>0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}
		 out.flush();
		 out.close();
	}
}
